package b4.view
{
	import b4.ApplicationFacade;
	import b4.model.ResourceLoader;
	import b4.model.TownProxy;
	import b4.view.component.admin.PaintDialog;
	import b4.view.component.admin.Painter;
	
	import flash.events.Event;
	
	import org.aswing.event.PopupEvent;
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class PaintDialogMediator extends Mediator implements IMediator
	{
		public static const NAME:String = 'PaintDialogMediator';
		public static const SHOW_INFO:String = NAME + 'ShowInfo';
		
		public function PaintDialogMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				TownSceneMediator.LOAD_TOWN_LAYER,
				PaintDialogMediator.SHOW_INFO
			];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			var name:String = notification.getName();
			var body:Object = notification.getBody();
			var type:String = notification.getType();
			switch ( name )
			{
				case TownSceneMediator.LOAD_TOWN_LAYER:
					paintDialog.loadTown(body.townIndex, body.layerIndex);
				break;
				case PaintDialogMediator.SHOW_INFO:
					paintDialog.infoLable.setText(body.toString());
				break;
			}
		}
		
		override public function onRegister():void
		{
			paintDialog.show();
			paintDialog.addEventListener(PopupEvent.POPUP_CLOSED, onPopUpClosed);
			paintDialog.loadLayerButton.addActionListener(onLoadLayerButtonClick);
			paintDialog.loadTownButton.addActionListener(onLoadTownButtonClick);
			paintDialog.editModeButton.addActionListener(onEditModeButtonClick);
		}
		
		private function onLoadLayerButtonClick(event:Event):void
		{
			var townItemName:String = paintDialog.townCombo.getSelectedItem();
			var townIndex:int = parseInt(townItemName.split(" ")[0]);
			var layerItemName:String = paintDialog.layerCombo.getSelectedItem();
			var layerIndex:int = parseInt(layerItemName.split(" ")[0]);
			sendNotification(TownSceneMediator.LOAD_TOWN_LAYER, {"townIndex": townIndex, "layerIndex": layerIndex});  
		}
		
		private function onLoadTownButtonClick(event:Event):void
		{
			var itemName:String = paintDialog.townCombo.getSelectedItem();
			var townIndex:int = parseInt(itemName.split(" ")[0]);
			sendNotification(ApplicationFacade.LOAD_TOWN_RESOURCE, {"townIndex": townIndex}); 
		}
		
		private function onEditModeButtonClick(event:Event):void
		{
			sendNotification(TownSceneMediator.SET_EDIT_MODE, paintDialog.editModeButton.isSelected());
		}
		
		private function onPopUpClosed(event:Event):void
		{
			facade.removeMediator(NAME);
		}
		
		override public function onRemove():void
		{
			paintDialog.dispose();
			Painter.penMode = null;
		}
		
		private function get paintDialog():PaintDialog
		{
			return viewComponent as PaintDialog;
		}
		
		private function get townProxy():TownProxy
		{
			return facade.retrieveProxy( TownProxy.NAME ) as TownProxy;
		}
	}
}